// -------------------------------------------------------------------
// markItUp!
// -------------------------------------------------------------------
// Copyright (C) 2008 Jay Salvat
// http://markitup.jaysalvat.com/
// -------------------------------------------------------------------
// MarkDown tags example
// http://en.wikipedia.org/wiki/Markdown
// http://daringfireball.net/projects/markdown/
// -------------------------------------------------------------------
// Feel free to add more tags
// -------------------------------------------------------------------
mySettings = {
	previewParserPath:  '/w/preview',
	previewParserVar:   'data',
	// previewAutoRefresh:  true,
	onShiftEnter:  	{keepDefault:false, replaceWith:'\n    '},
	onCtrlEnter:  	{keepDefault:false, replaceWith:'</br>\n'},
	onTab:    		{keepDefault:false, replaceWith:'    '},
	markupSet: [
		{name:'First Level Heading', key:'1', placeHolder:'title', closeWith:function(markItUp) { return miu.markdownTitle(markItUp, '=') } },
		{name:'Second Level Heading', key:'2', placeHolder:'title', closeWith:function(markItUp) { return miu.markdownTitle(markItUp, '-') } },
		{name:'Heading 3', key:'3', openWith:'### ', placeHolder:'title' },
		{name:'Heading 4', key:'4', openWith:'#### ', placeHolder:'title' },
		{name:'Heading 5', key:'5', openWith:'##### ', placeHolder:'title' },
		{name:'Heading 6', key:'6', openWith:'###### ', placeHolder:'title' },
		{separator:'---------------' },		
		{name:'Bold', key:'B', openWith:'**', closeWith:'**'},
		{name:'Italic', key:'I', openWith:'_', closeWith:'_'},
		{name:'Stroke through', key:'D', openWith:'<del>', closeWith:'</del>' },
		{separator:'---------------' },
		{name:'Bulleted List', openWith:'- ' },
		{name:'Numeric List', openWith:function(markItUp) {
			return markItUp.line+'. ';
		}},
		{separator:'---------------' },
		{name:'Picture', key:'P', replaceWith:'![[![Alternative text]!]]([![Url:!:http://]!] "[![Title]!]")'},
		{name:'Link', key:'L', openWith:'[', closeWith:']([![Url:!:http://]!] "[![Title]!]")', placeHolder:'Your text to link here...' },
		{separator:'---------------'},	
		{name:'Quotes', replaceWith:function(markitup) { return miu.indentText(markitup); }},
		//{name:'Code Block / Code', openWith:'(!(\t|!|`)!)', closeWith:'(!(`)!)'},
		//{name:'Code Block / Code', openWith:'~~~.{brush: text;}\n', closeWith:'\n~~~\n' },
		{name:'Code Block / Code', replaceWith:function(markitup) { return miu.smartCodeBlock(markitup); } },
		{name:'Horizontal rules', closeWith:'\n---------------------------------------\n'},
		{separator:'---------------' },
		{name:'Clean', className:'clean', replaceWith:function(markitup) { return markitup.selection.replace(/<(.*?)>/g, "") } },		
		{name:'Preview', className:'preview', key:'S', call:'preview'}
	]
}

// mIu nameSpace to avoid conflict.
miu = {
	markdownTitle: function(markItUp, char) {
		heading = '';
		n = $.trim(markItUp.selection||markItUp.placeHolder).length;
		for(i = 0; i < n; i++) {
			heading += char;
		}
		return '\n'+heading;
	},
	indentText: function(markItUp){
		out = '';
		text = $.trim(markItUp.selection||markItUp.placeHolder);
		n = text.length;
		for(i = 0; i < n; i++) {
			c = text.charAt(i);
			out += c;
			if(c=='\n'){
				out += '    ';
			}
		}
		return '\n    '+out;
	},
	smartCodeBlock:function(markItUp){
		max_line = 10; // if line count larger then max_line,then provide more options, default to collapse.
		text = $.trim(markItUp.selection||markItUp.placeHolder);
		line_count = 0;
		head = '~~~.{brush: shell;}\n'
		for(i = 0; i < text.length; i++) {
			c = text.charAt(i);
			if(c=='\n'){
				line_count += 1;
				if(line_count>max_line)
				{
					head = '~~~.{brush: shell;toolbar: true;collapse: true;first-line: 1; highlight: []}\n'
					break;
				}
			}
		}
		return head + text + '\n~~~\n'
	}
} 